package com.chuanye.mydrawable.adapters;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.HorizontalScrollView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chuanye.mydrawable.R;
import com.chuanye.mydrawable.activitys.QueryRepairActivity;
import com.chuanye.mydrawable.beans.RepairInfoBean;
import com.chuanye.mydrawable.utils.DateUtils;
import com.chuanye.mydrawable.views.CustomHScrollView;

import java.text.ParseException;
import java.util.List;

/**
 * Created by lsef on 2019/2/1.
 */

public class RereConAdapter extends BaseAdapter{
    private Context mContext;
    private List<RepairInfoBean.ListBean> listBeanList;
    private String strr;
    private RelativeLayout mHead;
    private int n = 1;//标记变量

    public RereConAdapter(Context mContext, List<RepairInfoBean.ListBean> listBeanList,RelativeLayout head) {
        this.mContext = mContext;
        this.listBeanList = listBeanList;
        this.mHead = head;
    }

    @Override
    public int getCount() {
        return listBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return listBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null){
            view = View.inflate(mContext, R.layout.repair_record_con_list_view,null);
            holder = new ViewHolder();
            CustomHScrollView scrollView = (CustomHScrollView) view.findViewById(R.id.h_scrollView);
            holder.scrollView = scrollView;
            holder.rerecon_number_tv = view.findViewById(R.id.rerecon_number_tv);//报修编号
            holder.rerecon_repairman_tv = view.findViewById(R.id.rerecon_repairman_tv);//报修人
            holder.rerecon_receiverman_tv = view.findViewById(R.id.rerecon_receiverman_tv);//受理人
            holder.rerecon_time_tv = view.findViewById(R.id.rerecon_time_tv);//时间
            holder.rerecon_step_tv = view.findViewById(R.id.rerecon_step_tv);//当前步骤

            CustomHScrollView headSrcrollView = (CustomHScrollView) mHead.findViewById(R.id.h_scrollView);
            headSrcrollView.AddOnScrollChangedListener(new OnScrollChangedListenerImp(scrollView));
            view.setTag(holder);
        }
        holder = (ViewHolder) view.getTag();

        //报修时间
        String repairtime = listBeanList.get(i).getRepairTime();
        String strsc =repairtime.substring(0,10);
        Log.i("打印：",strsc);
        try {
            holder.rerecon_time_tv.setText(DateUtils.getStringToDate(DateUtils.getDateToString(strsc)));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        holder.rerecon_number_tv.setText(listBeanList.get(i).getRepairId());//报修编号
        holder.rerecon_repairman_tv.setText(listBeanList.get(i).getRepairPersonnel());//报修人
        holder.rerecon_receiverman_tv.setText(listBeanList.get(i).getRMuserName());//受理人
        //holder.rerecon_step_tv.setBackgroundColor(R.color.white);
        if (listBeanList.get(i).getOperator() == 0){
            holder.rerecon_step_tv.setText("报修服务");//当前步骤

        }else if (listBeanList.get(i).getOperator() == 2){
            holder.rerecon_step_tv.setText("远");//当前步骤
            //holder.rerecon_step_tv.setBackgroundColor(Color.parseColor("#FFF46C6C"));
            holder.rerecon_step_tv.setBackgroundResource(R.drawable.circle_yuan_bg);
        }else if (listBeanList.get(i).getOperator() == 3){
            holder.rerecon_step_tv.setText("现");//当前步骤
            //holder.rerecon_step_tv.setBackgroundColor(Color.parseColor("#FF67C13A"));
            holder.rerecon_step_tv.setBackgroundResource(R.drawable.circle_xian_bg);
        }
        else if (listBeanList.get(i).getOperator() == 4){
            holder.rerecon_step_tv.setText("换");//当前步骤
            holder.rerecon_step_tv.setBackgroundColor(Color.parseColor("#FF409DFE"));
            holder.rerecon_step_tv.setBackgroundResource(R.drawable.circle_huan_bg);
        }
        else if (listBeanList.get(i).getOperator() == -1){
            holder.rerecon_step_tv.setText("完");//当前步骤
            holder.rerecon_step_tv.setBackgroundColor(Color.parseColor("#FFE5A13C"));
            holder.rerecon_step_tv.setBackgroundResource(R.drawable.circle_wan_bg);
        }
        holder.rerecon_step_tv.setTextColor(Color.parseColor("#FFFFFF"));
        return view;
    }

    class OnScrollChangedListenerImp implements CustomHScrollView.OnScrollChangedListener {
        CustomHScrollView mScrollViewArg;

        public OnScrollChangedListenerImp(CustomHScrollView scrollViewar) {
            mScrollViewArg = scrollViewar;
        }

        @Override
        public void onScrollChanged(int l, int t, int oldl, int oldt) {
            mScrollViewArg.smoothScrollTo(l, t);
            if (n == 1) {//记录滚动的起始位置，避免因刷新数据引起错乱
                new QueryRepairActivity().setPosData(oldl, oldt);
            }
            n++;
        }
    };

    private static class ViewHolder{
        TextView rerecon_number_tv;//报修编号
        TextView rerecon_repairman_tv;//报修人
        TextView rerecon_receiverman_tv;//受理人
        TextView rerecon_time_tv;//时间
        TextView rerecon_step_tv;//当前步骤
        HorizontalScrollView scrollView;
    }
}
